From adccd1391efa49faf82abe48bf52e5c9813844d2 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Wed, 5 Feb 2020 17:44:47 +0100 Subject: [PATCH] Support aspect=TRUE in _gdk_pixbuf_new_from_stream_at_scale The icon theme test failed without this, and we *should* handle it if we're accepting the argument. --- gtk/tools/gdkpixbufutils.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/gtk/tools/gdkpixbufutils.c b/gtk/tools/gdkpixbufutils.c index c706d720a8..0d93b3237e 100644 --- a/gtk/tools/gdkpixbufutils.c +++ b/gtk/tools/gdkpixbufutils.c @@ -126,7 +126,28 @@ size_prepared_cb2 (GdkPixbufLoader *loader, { int *scales = data; - gdk_pixbuf_loader_set_size (loader, scales[0], scales[1]); + if (scales[2]) /* keep same aspect ratio as original, while fitting in given size */ + { + double aspect = (double) height / width; + + /* First use given width and calculate size */ + width = scales[0]; + height = scales[0] * aspect; + + /* Check if it fits given height, otherwise scale down */ + if (height > scales[1]) + { + width *= (double) scales[1] / height; + height = scales[1]; + } + } + else + { + width = scales[0]; + height = scales[1]; + } + + gdk_pixbuf_loader_set_size (loader, width, scales[1]); } GdkPixbuf * @@ -140,7 +161,7 @@ _gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream, { GdkPixbufLoader *loader; GdkPixbuf *pixbuf; - int scales[2]; + int scales[3]; if (format) { @@ -153,6 +174,7 @@ _gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream, scales[0] = width; scales[1] = height; + scales[2] = aspect; g_signal_connect (loader, "size-prepared", G_CALLBACK (size_prepared_cb2), scales); -- 2.30.2